<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Alliance extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->library('Mydatetime');
		$this->load->library('session');
		$this->load->model('alliance/alliance_model');
	}
    public function index()
	{
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        
        $data = array();
        $uid = $this->session->userdata('uid');
        $info = $this->alliance_model->ww_get_alliance($uid);
        $data['info'] = $info['0'];
        $data['lstMem'] = $info['1'];
        $data['uid'] = $uid;
        // header
        
        // content
        $this->load->view('alliance/alliance_view', $data);
        
        // foter
    }
    public function create()
	{
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        if($this->session->userdata('allianceid'))
        {
            redirect(site_url('alliance'));
            die();
        }
        $data = array();
        $error = array();
        $error['alliance_name'] = '';
        $error['alliance_abbreviated'] = '';
        $uid = $this->session->userdata('uid');
        $info = $this->alliance_model->ww_get_alliance($uid);
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
           $info = array();
           $info['alliance_name'] = $this->input->post('alliance_name');
           $info['alliance_abbreviated'] = $this->input->post('alliance_abbreviated');
           $info['alliance_desc'] = $this->input->post('alliance_desc');
           $alliance_name = $this->mycommon->getSaveSqlStr(trim($info['alliance_name']));
           $alliance_abbreviated = $this->mycommon->getSaveSqlStr(($info['alliance_abbreviated']));
           $alliance_desc = $this->mycommon->getSaveSqlStr(trim($info['alliance_desc']));
           
           $chkName = strlen($alliance_name) > 80 ? false : true;
           $chkAbb = strlen($alliance_abbreviated) > 10 ? false : true;
           
           $error['alliance_name'] = $chkName == false ? 'Tên liên minh quá dài' : '';
           $error['alliance_abbreviated'] = $chkAbb == false ? 'Tên viết tắt quá dài' : '';
           
           if($chkName == true && $chkAbb == true)
           {
            // insert sql
            $this->alliance_model->ww_create_alliance($uid, $alliance_name, $alliance_abbreviated, $alliance_desc);
            echo 'done';
           }
        }
        $data['error'] = $error;
        
        // header
        
        // content
        $this->load->view('alliance/create_alliance_view', $data);
        
        // footer
    }
    function rev_member()
    {
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        $data = array();
        $time = time();
        // check level
        
        
        $this->alliance_model->ww_remove_member_alliance($uid, $member, $time);
        
    }
}